package com.alibaba.citrus.cr.mn.order.center.facade.function.common;


import cn.hutool.core.date.StopWatch;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomCloseResponse;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangxiaorui
 * @ClassName OrderBizCreateService
 * @description: TODO
 * @date 2023/05/16 11:28
 * @version: 1.0
 */
public interface OrderBizCloseService<BizInput, AtomInput, BizOutput> {

    Log log = Log.getLogger(OrderBizCloseService.class);

    default Result<List<BizOutput>> batchClose(BizInput bizBatchCloseRequest) {
        StopWatch stopWatch = new StopWatch("OrderBizCloseService");
        try {
            // 1.0 参数预处理，订单头公共属性 同步更新至 订单明细
            stopWatch.start("preHandleRequestParams");
            preHandleRequestParams(bizBatchCloseRequest);
            stopWatch.stop();

            // 2.0 非空，长度，枚举校验
            stopWatch.start("paramsInvalidCheck");
            paramsInvalidCheck(bizBatchCloseRequest);
            stopWatch.stop();

            // 3.0 业务校验-防腐适配处理 以及基础数据转换
            stopWatch.start("assembleBizBaseInfo");
            List<OrderAtomCloseResponse> responses = assembleBizBaseInfo(bizBatchCloseRequest);
            stopWatch.stop();

            // 4.0 商业Request->原子Request  一般是商业继承自原子，没特殊变化，可以直接返回入参
            stopWatch.start("convertAtomRequest");
            AtomInput atomCreateRequest = convertAtomRequest(bizBatchCloseRequest);
            stopWatch.stop();

            // 5.0 调用订单原子创建能力
            stopWatch.start("processAtomAbility");
            Result<List<BizOutput>> result = processAtomAbility(atomCreateRequest, responses);
            stopWatch.stop();

            return result;
        } catch (Exception e) {
            log.error("OrderBizCloseService#batchClose error", e);
            throw e;
        } finally {
            if (stopWatch.isRunning()) stopWatch.stop();
            log.info("OrderBizCloseService#batchClose cost time:{}", stopWatch.prettyPrint(TimeUnit.MILLISECONDS));
        }
    }

    void paramsInvalidCheck(BizInput bizBatchCloseRequest);

    void preHandleRequestParams(BizInput bizBatchCreateRequest);

    List<OrderAtomCloseResponse> assembleBizBaseInfo(BizInput bizOrderCreateList);

    AtomInput convertAtomRequest(BizInput bizBatchCreateRequest);

    Result<List<BizOutput>> processAtomAbility(AtomInput atomCreateRequest, List<OrderAtomCloseResponse> responses);
}
